$(function(){
	var userName = sessionStorage['username'];
	if(userName==null||userName==""){
		$(window).mobileMsgBox({msgText:"请先登录",delayTime:1000});
		window.location="login.html#loginPage";
	}
	$("#home").bind("tap",toHome);
	$("#gridPage").mobileGrid({pageSize:5,currentPageIndex:1,userId:sessionStorage['userId'],server:localStorage['APP_AJAX_PATH']+"/drinkingBar/view/addressManagement.jsp.findAddressByUserId"});
	$("#addBtn").bind("tap",function(){
		androidLocal({"type":1});
		window.location="addAddress.html";
	});
	$("#updateBtn").bind("tap",function(){
		var rows=$("#gridPage").selectedRecords();
		 if (!rows || rows.length != 1) {
			 $(window).mobileMsgBox({msgText:"请选择一行",delayTime:500});
				return;
			    }
		androidLocal({"type":1});
		androidLocal({"addressId":rows[0].id});
		window.location="updateAddress.html";
	  	
   });
	$("#delBtn").bind("tap",function(){
		var rows=$("#gridPage").selectedRecords();
		 if (!rows || rows.length != 1) {
			 $(window).mobileMsgBox({msgText:"请选择一行",delayTime:500});
				return;
			    }
		 Newtouch.Ajax.sendParams({addressId:rows[0].id},localStorage['APP_AJAX_PATH']+"/drinkingBar/view/addressManagement.jsp.deleteAddress",function(){
         	$(window).mobileMsgBox({msgText:"删除成功",delayTime:500});
             window.location="addressManagement.html";
		 },function(err){
	    	});
	  	
   });
	$("#setDefaultBtn").bind("tap",function(){
		var rows=$("#gridPage").selectedRecords();
		 if (!rows || rows.length != 1) {
			 $(window).mobileMsgBox({msgText:"请选择一行",delayTime:500});
				return;
			    }
		 Newtouch.Ajax.sendParams({addressId:rows[0].id,userId:sessionStorage['userId']},localStorage['APP_AJAX_PATH']+"/drinkingBar/view/addressManagement.jsp.setDefaultAddress",function(){
         	$(window).mobileMsgBox({msgText:"设置成功",delayTime:500});
             window.location="addressManagement.html";
		 },function(err){
	    	});
	  	
   });
	

});

function address(cell,data){
	if(data.addrState == 1){
		var html='【默认】'+data.deliveryPersonNm+data.deliveryMobile+'</br>'+data.city_name+data.area_name+data.street_name+data.detail+'';
		cell.html(html);
	}else{
		var html=''+data.deliveryPersonNm+data.deliveryMobile+'</br>'+data.city_name+data.area_name+data.street_name+data.detail+'';
		cell.html(html);	
	}
	
}
function editOrDelete(cell,data){
	var html='<b class="ui-mobile-grid-title">操作</b><a class="btn" onclick=editRecord("'+data.id+'")><img src="resource/image/gouwuche_32.png"></img>';
	cell.html(html);
}

function editRecord(id){         
	Newtouch.Ajax.sendParams({productId:id},localStorage['APP_AJAX_PATH']+"/drinkingBar/view/addressManagement.jsp.addProToCart",function(data){	
		$(window).mobileMsgBox({msgText:"添加购物车成功",delayTime:500});
	},function(err){
		$(window).mobileMsgBox({msgText:"添加购物车失败",delayTime:500});
	});
    
}
var androidLocal=function(p){
	//sessionStorage.clear(); //清空已有记录，保存新纪录
	for(var k in p){
		sessionStorage[k]=p[k];
	}
	
}

function toHome(){
	window.location = "main.html";
}
